package cn.flant.log.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleLogger extends LogAdapter {

	private DateFormat format = new SimpleDateFormat("[MM-dd HH:mm:ss]");

	protected SimpleLogger(String name) {
		super(name);
	}

	public void debug(Object msg) {
		if (DEBUG < level) {
			return;
		}
		System.out.print(fromatMessage(msg, "DEBUG"));
	}

	public void debug(Object msg, Throwable t) {
		if (DEBUG < level) {
			return;
		}
		System.out.print(fromatMessage(msg, "DEBUG"));
		t.printStackTrace();
	}

	public void error(Object msg) {
		if (ERROR < level) {
			return;
		}
		System.err.print(fromatMessage(msg, "ERROR"));
	}

	public void error(Object msg, Throwable t) {
		if (ERROR < level) {
			return;
		}
		System.err.print(fromatMessage(msg, "ERROR"));
		t.printStackTrace();
	}

	public void fatal(Object msg) {
		if (FATAL < level) {
			return;
		}
		System.err.print(fromatMessage(msg, "FATAL"));
	}

	public void fatal(Object msg, Throwable t) {
		if (FATAL < level) {
			return;
		}
		System.err.print(fromatMessage(msg, "FATAL"));
		t.printStackTrace();
	}

	public void info(Object msg) {
		if (INFO < level) {
			return;
		}
		System.out.print(fromatMessage(msg, "INFO"));
	}

	public void info(Object msg, Throwable t) {
		if (INFO < level) {
			return;
		}
		System.out.print(fromatMessage(msg, "INFO"));
		t.printStackTrace();
	}

	public void warn(Object msg) {
		if (WARN < level) {
			return;
		}
		System.err.print(fromatMessage(msg, "WARNING"));
	}

	public void warn(Object msg, Throwable t) {
		if (WARN < level) {
			return;
		}
		System.err.print(fromatMessage(msg, "WARNING"));
		t.printStackTrace();
	}

	private synchronized String fromatMessage(Object msg, String level) {
		return format.format(new Date()) + " " + level + " - " + msg + "\n";
	}
}